<?php 

$statuses = array (
	'1'=>'In Progress',
	'2'=>'Ready for Review',
	'3'=>'See Dev Notes',
	'4'=>'Ready for copyedit',
	'5'=>'Discussion Needed',
	'7'=>'See Hudson changes',
	'8'=>'Hudson review',
	'6'=>'FINAL'
	);   

function db_connect() {

	 $link = mysql_connect('66.118.188.130', 'ginkgo', 'dig4wups9uj5pa');
	 if (!$link) {
	  die ('Not connected : ' . mysql_error());
	 }
	 $db_selected = mysql_select_db('ginkgo', $link);
	
	 if (!$db_selected) {
	  die ('Can\'t use foo : ' . mysql_error());
	 }
 
}

 /*
function my_wp_plugin_tag_action($content,$tag,$function,$args = FALSE) {
  // match all regular expressions
  preg_match_all($tag,$content,$matches);
  if (count($matches)>0) {
    // filter duplicates
    $matches = array_unique($matches);
    // loop through
    $tag_results = array();
    $found_tags = array();
    foreach ($matches as $idx => $match) {
      //build arg array
      $full_tag = array_shift($match);
      //call function, adding function output and full tag text to replacement array
      $tag_results[] = my_wp_plugin_buffer_func($function,$match);
      $found_tags[] = $full_tag;
    }
    // replace all tags with corresponding text
    $content = str_replace($found_tags,$tag_results,$content);
  }
  return $content;
}

function my_wp_plugin_buffer_func($funcname,$args = false) {
  if (!$args) $args = array();
  ob_start();
  call_user_func_array($funcname,$args);
  $output = ob_get_contents();
  ob_end_clean();
  return $output;
}
*/

function parse_crossReference ($string,$section_number,$chapter_id, $clear = 0){
	$string = str_replace ("&lt;","<", $string );
	$string = str_replace ("&gt;",">", $string ); 
	
	//echo "|||$string|||";
	echo db_connect();
	preg_match_all("<<[0-9]+(?:\.[0-9]*)?>>",$string,$matches);
	foreach ($matches[0] as $val) {
		$link_name = "";  
		$a = str_replace('<','',$val);
		$a = str_replace('>','',$a);
		$full_string = "<<".$a.">>";
		
		$sql = "SELECT c.title, s.title, s.detail_title, s.section_num,c.documentation_chapter_id
		FROM documentation_chapters c, documentation_sections s
		WHERE s.documentation_chapter_id = c.documentation_chapter_id
		AND s.section_id = '$a'";
		$result = mysql_query($sql);
		if (!$row = mysql_fetch_array($result)) echo mysql_error();
       if ($row[3] == $section_number && $row[4] == $chapter_id) {  
			// short form cross-ref with detail title only
			//$link = "#"; // link to anchor
			$link = "javascript: scrollGo($a);"; // link to anchor   
		 } else { // long form with chapter & section
	    	$link_name .=  $row[0];
	    	$link_name .=  "> ". $row[1] . ">";
			$link = "index.php?s=$a"; // reload page 
		  }
		 if ($row[2]) { $link_name .= $row[2]; } else { $link_name = rtrim($link_name,'>'); }
		 
		 if ($clear) {
		   $string = str_replace ( $full_string , " [link] " , $string );
		 }else{
		   $string = str_replace ( $full_string , "<span class='xref'><a href='$link'>$link_name</a></span>" , $string );
		 }
		 
		   
	}
	return $string; 
}

?>